// pages/help/select_other_family_member.js
const app = getApp()
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    family_id: 0,  //当前所选家族id
    person_lst: [],  //人员列表
    show_person_lst: [],  //考虑搜索后的人员列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      family_id: parseInt(options.family)
    })
    let sex_name = ''
    if (app.other_family_member_select_sex == 0) {
      sex_name = '女性'
    } else if (app.other_family_member_select_sex == 1) {
      sex_name = '男性'
    }

    wx.setNavigationBarTitle({
      title: '选择家族中的' + sex_name + '人员',
    })

    app.httpRequest('get_family_members', {
      family: this.data.family_id
    }, res => {
      console.log('family members: ', res)
      let arr = []
      if (app.other_family_member_select_sex == 0 || app.other_family_member_select_sex == 1) {
        for (let i = 0; i < res.length; i++) {
          if (res[i].person_sex == app.other_family_member_select_sex) {
            arr.push(res[i])
          }
        }
      } else {
        arr = res
      }
      this.setData({
        person_lst: arr,
        show_person_lst: arr
      })
      this.setData({
        search: this.search.bind(this)
      })

    }, res => {
      console.log('get family members failed: ', res)
    })
  },

  //选择人员
  RadioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)

    let items = this.data.show_person_lst
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].person_id == e.detail.value
    }
  },

  bindConfirm(e) {
    let select_person = 0
    let items = this.data.show_person_lst
    for (let i = 0, len = items.length; i < len; ++i) {
      if (items[i].checked) {
        select_person = items[i].person_id
        break
      }
    }

    if (0 == select_person) {
      app.showTip('请选择人员')
      return
    }

    wx.navigateBack({
      delta: 0,
    })
    let pages = getCurrentPages()  //获取当前页面js里面的pages里的所有信息。
    let prevPage = pages[pages.length - 2]  //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。
    prevPage.selectPersonCallback(this.data.family_id, select_person)
  },

  search: function (value) {
    console.log('search value: ', value)
    value = value.trim()
    if (value.length == 0) {
      this.setData({
        show_person_lst: this.data.person_lst
      })
    } else {
      let arr = []
      let person_lst = this.data.person_lst
      for (let i = 0; i < person_lst.length; i++) {
        if (person_lst[i].person_name.indexOf(value) != -1 || person_lst[i].person_nick.indexOf(value) != -1) {
          arr.push(util.deepCopy(person_lst[i]))
        }
      }
      this.setData({
        show_person_lst: arr
      })
    }

    //正常返回
    return new Promise((resolve, reject) => {
      resolve([])
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})